package me.app.home;

import me.app.global.MyApplication;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import com.tencent.tencentmap.lbssdk.TencentMapLBSApi;
import com.tencent.tencentmap.lbssdk.TencentMapLBSApiListener;
import com.tencent.tencentmap.lbssdk.TencentMapLBSApiResult;

public class MyLocationService extends Service {

	private LocListener mListener = null;
	private MyApplication mApplication = null;
	private TencentMapLBSApiResult mLocRes = null;
	private TencentMapLBSApiResult mOldLocRes = null;

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		this.mListener = new LocListener(TencentMapLBSApi.GEO_TYPE_GCJ02,
				TencentMapLBSApi.LEVEL_ADMIN_AREA, 0);
		// TencentMapLBSApi.getInstance().setGPSUpdateInterval(6000);
		this.mApplication = (MyApplication) this.getApplication();
		int req = TencentMapLBSApi.getInstance().requestLocationUpdate(
				this.getApplicationContext(), mListener);
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		Log.i("service2", "我在服务中啊！");
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		TencentMapLBSApi.getInstance().removeLocationUpdate();
		super.onDestroy();
	}

	private class LocListener extends TencentMapLBSApiListener {

		public LocListener(int reqGeoType, int reqLevel, int reqDelay) {
			super(reqGeoType, reqLevel, reqDelay);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onLocationUpdate(TencentMapLBSApiResult locRes) {
			// TODO Auto-generated method stub
			mOldLocRes = mLocRes;
			mLocRes = locRes;
			if ((mOldLocRes == null && mLocRes != null)
					|| (mLocRes != null && cmpTwoRes(mLocRes, mOldLocRes))) {
				// updateMapView(locRes);
				Log.i("service", "我变化了，来自于服务！" + locRes.toString());
			}
		}

		@Override
		public void onStatusUpdate(int arg0) {
			// TODO Auto-generated method stub
			super.onStatusUpdate(arg0);
		}

	}

	private boolean cmpTwoRes(TencentMapLBSApiResult resA,
			TencentMapLBSApiResult resB) {
		if (resA != null && resB != null) {
			if (resA.Latitude - resB.Latitude >= 0.015
					|| resA.Longitude - resB.Longitude >= 0.015) {
				return true;
			}
		}
		return false;
	}

//	private void updateMapView(TencentMapLBSApiResult mLocRes) {
//		// TODO Auto-generated method stub
//		if (null != MyApplication.mMapView && null != mLocRes) {
//			MyApplication.mMapView.clearAllOverlays();
//			MyApplication.mMapView.getController().setCenter(
//					new GeoPoint((int) (mLocRes.Latitude * 1E6),
//							(int) (mLocRes.Longitude * 1E6)));
//			MyHomeActivity.myOverlay.setLat(mLocRes.Latitude);
//			MyHomeActivity.myOverlay.setLng(mLocRes.Longitude);
//			MyApplication.mMapView.addOverlay(MyHomeActivity.myOverlay);
//			List<TencentMapLBSApiResult> results = new ArrayList<TencentMapLBSApiResult>();
//			results.add(mLocRes);
//			MyHomeActivity.myItemOverlay.setLbsRes(results);
//			MyApplication.mMapView.addOverlay(MyHomeActivity.myItemOverlay);
//			MyApplication.mMapView.getController().setZoom(14);
//
//		}
//	}

}
